package com.eam.handler;

import com.eam.data.*;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Query;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.SessionInfo;
import org.openbravo.model.ad.access.User;
import java.util.ArrayList;
import java.util.List;

public class EmesoFacilitiesCollectionHandlerService {
	/**
	 * 处理勾选完成的维保任务
	 */
	public void confirmMaintainPlan(String str, String actionType) {
		// 用户ID
		User u = OBDal.getInstance().get(User.class, SessionInfo.getUserId());
		String itemID = "";
		String Description = "";
		String[] IDArg = str.split("#####");
		for (String id : IDArg) {
			String[] details = id.split("@@");
			EamMaintainPlanItems empd = OBDal.getInstance().get(EamMaintainPlanItems.class, details[0]);
			// 获取ItemID用于存储过程修改最后一次维保时间
			if (itemID == "") {
				itemID = empd.getEamMaintainPlanFacs().getId();
			}
			empd.setStatus("OK");
			if (details.length == 2) {
				empd.setDescription(details[1]);
				Description = details[1];
			}
			insertLogInfo(empd.getEamMaintainPlanFacs().getEamFacilities(), empd.getEamMaintainPlanFacs()
					.getEamMaintainPlan(), u, empd.getEamMaintainItem(), empd.getEamRepairItem(), Description);
			OBDal.getInstance().save(empd);
		}
		// 刷入数据库
		OBDal.getInstance().flush();
		// 调用存储过程
		String sql = "{call Emes_P_UPDATE_EmesMPD(?)}";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql);
		query.setString(0, itemID);
		query.executeUpdate();
	}
	
	/**
	 * 选择维保计划打开界面
	 * @param id
	 * @return
	 */
	public JSONObject getPlanItem(String id, String Type) throws JSONException {
		JSONObject jsonRep = new JSONObject();
		// JSONObject
		EamMaintainPlan plan = null;
		List<EamMaintainPlanFacs> list = new ArrayList<EamMaintainPlanFacs>();
		List<JSONObject> fancilityList = new ArrayList<JSONObject>();
		if (Type.equalsIgnoreCase("OBPOPWINDOWPLAN")) {
			plan = OBDal.getInstance().get(EamMaintainPlan.class, id);
			list = plan.getEamMaintainPlanFacsList();
		} else {
			EamMaintainPlanFacs empi = OBDal.getInstance().get(EamMaintainPlanFacs.class, id);
			plan = empi.getEamMaintainPlan();
			list.add(empi);
		}
		int count = 0;
		if (list == null) {
			jsonRep.put("flag", false);
			jsonRep.put("message", "该计划下无维护设备或计划已完成");
		} else {
			// 返回值为plan
			jsonRep.put("type", "PLAN");
			jsonRep.put("name", plan.getName());
			jsonRep.put("code", plan.getCode());
			// 循环获取维保设备下维保项目
			for (EamMaintainPlanFacs obj : list) {
				if (obj.getStatus().equalsIgnoreCase("ok")) {
					continue;
				}
				;
				JSONObject json = new JSONObject();
				EamFacilities ef = obj.getEamFacilities();
				json.put("name", ef.getName());
				json.put("code", ef.getCode());
				json.put("id", ef.getId());
				json.put("details", ef.getDescription());
				if (ef.getStatus().equalsIgnoreCase("FREE")) {
					json.put("status", "封存");
				} else if (ef.getStatus().equalsIgnoreCase("USE")) {
					json.put("status", "正常");
				} else if (ef.getStatus().equalsIgnoreCase("STOP")) {
					json.put("status", "停用");
				}
				if (ef.getType().equalsIgnoreCase("10")) {
					json.put("type", "设备");
				} else if (ef.getType().equalsIgnoreCase("20")) {
					json.put("type", "工装");
				} else if (ef.getType().equalsIgnoreCase("30")) {
					json.put("type", "模具");
				}
				json.put("user", obj.getUser().getName());
				json.put("org", ef.getOrganization().getName());
				// 获取维保项目
				List<EamMaintainPlanItems> details = obj.getEamMaintainPlanItemsList();
				List<JSONObject> maintainList = new ArrayList<JSONObject>();
				List<JSONObject> repairList = new ArrayList<JSONObject>();
				for (EamMaintainPlanItems empd : details) {
					if (!empd.getStatus().equalsIgnoreCase("ok")) {
						if (empd.getType().equalsIgnoreCase("MAINTAIN")) {
							JSONObject Maintain = new JSONObject();
							Maintain.put("name", empd.getEamMaintainItem().getName());
							Maintain.put("id", empd.getId());
							Maintain.put("code", empd.getEamMaintainItem().getCode());
							Maintain.put("r_id", empd.getEamMaintainItem().getId());
							maintainList.add(Maintain);
							count += 1;
						} else {
							JSONObject Repair = new JSONObject();
							Repair.put("name", empd.getEamRepairItem().getName());
							Repair.put("id", empd.getId());
							Repair.put("code", empd.getEamRepairItem().getCode());
							Repair.put("r_id", empd.getEamRepairItem().getId());
							repairList.add(Repair);
							count += 1;
						}
					}
				}
				json.put("maintainList", maintainList);
				json.put("repairList", repairList);
				fancilityList.add(json);
			}
			// 维保计划是否为空
			if (count == 0) {
				jsonRep.put("flag", false);
				jsonRep.put("desc", "该维保项目下无维保的项目或所有维保项目已完成");
				return jsonRep;
			}
			jsonRep.put("fancility", fancilityList);
			jsonRep.put("flag", true);
		}
		return jsonRep;
	}
	
	/**
	 * 插入维修保养日志表
	 */
	protected void insertLogInfo(EamFacilities EamFacilities, EamMaintainPlan maintainPlan, User user,
								 EamMaintainItem em, EamRepairItem er, String desc) {
		EamFacilitiesMTLogs log = new EamFacilitiesMTLogs();
		log.setEamFacilities(EamFacilities);
		log.setEamMaintainPlan(maintainPlan);
		log.setUser(user);
		if (em == null) {
			log.setEamRepairItem(er);
			log.setType("REPAIRMENT");
		} else {
			log.setEamMaintainItem(em);
			log.setType("MAINTAIN");
		}
		if (desc != null) {
			log.setDescription(desc);
		}
		log.setStatus("FINISHED");
		log.setUser(user);
		OBDal.getInstance().save(log);
	}
}
